package cn.admin.spring.mvc;

import cn.admin.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 13:28 2019/4/25
 */
@Configuration
@EnableWebMvc
@ComponentScan(value = {"cn.admin.controller"}) //扫描controller
public class SpringMvcConfig extends WebMvcConfigurationSupport {

    //拦截器
    @Autowired
    private LoginInterceptor loginInterceptor;


    /**
     * 配置Web
     *
     * @return
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurerAdapter() {
            /**
             * 注册拦截器
             * @param registry
             */
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(loginInterceptor).addPathPatterns("/**");   //拦截器拦截的路径为所有
            }

            /**
             * 静态资源配置
             * @param registry
             */
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //
                registry.addResourceHandler("/img/*")
                        .addResourceLocations("/img/");
                registry.addResourceHandler("/static/**")   //静态资源在哪个URL下
                        .addResourceLocations("/static/");  //静态资源的路径
            }

            /**
             * 配置视图解析器
             * @param registry
             */
            @Override
            public void configureViewResolvers(ViewResolverRegistry registry) {
                registry.jsp("/WEB-INF/views", ".jsp");
            }

            /**
             * 添加跨域自持
             * @param registry
             */
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")  //跨域访问的路径,也可以设置为:/api/**
                        .allowedOrigins("*")    //允许任何源访问
                        .allowedMethods("*")  //跨域访问的方式,允许所有
                        .allowedHeaders("*")    //允许头部为任何
                        .maxAge(1800) //预检间隔时间
                        .allowCredentials(true); //是否发送cookie
            }

            /**
             * 消息转换器,转编码格式
             * @param converters
             */
            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                //字符编码转换
                StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
                stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
                //支持JSON返回,否则报错
                MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();

                converters.add(stringHttpMessageConverter);
                converters.add(mappingJackson2HttpMessageConverter);
            }

        };
        return webMvcConfigurer;
    }

    /**
     * 上传文件配置
     *
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        //配置编码格式
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        //最大文件大小
        commonsMultipartResolver.setMaxUploadSize(524288000);
        return commonsMultipartResolver;
    }


    /**
     * 拦截器
     *
     * @return
     */
    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }


}
